Ext.define('cloud.controller.MessageControll', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        refs: {
            message: 'message',
            messageadd: 'messageadd',
            messageedit: 'messageedit',
            message_search: 'message #message_search',
            message_btn_add: 'message #message_btn_add',
            message_upload: 'messageadd #message_upload',
            message_edit_upload: 'messageedit #message_edit_upload',
            message_submit: 'messageadd #message_submit',
            message_edit: 'messageedit #message_edit',
            message_del: 'messageedit #message_del',
            messageSelect: 'message #messageSelect',
            userid: 'messageadd #message_cuid'
        },
        control: {
            userid: {
                focus: function () {
                    var Store = Ext.getStore('UsersAllFindStore');
                    Store.load({params: {caid: localStorage.getItem('caid')}});
                }
            },

            /**
             * show messageadd page
             */
            message_btn_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var message = this.getMessage();
                    var messageadd = this.getMessageadd()||Ext.create('cloud.view.message.MessageAdd');
                    Ext.Viewport.unmask();
                    message.push(messageadd);
                }
            },
            /**
             * show messageList
             */
            messageSelect: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var message = this.getMessage();
                    Ext.getStore('MessageDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                    var messageedit = this.getMessageedit()||Ext.create('cloud.view.message.MessageEdit');
                    messageedit.setRecord(record);
                    Ext.Viewport.unmask();
                    message.push(messageedit);
                }
            },
            /**
             * del message_image
             */
            message_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getMessageedit().submit({
                                url: GLOBAL.api_url + 'message_delete',
                                scope: this,
                                method: 'POST',
                                success: this.exesucess
                            });
                        }
                    }, this);
                }
            },
            /**
             * submit message
             */
            message_submit: {
                tap: function () {
                    if (Ext.isEmpty(Ext.getCmp('message_cadid').getValue()))
                        return Ext.Msg.alert('请输入所在开通地区ID');
                    if (Ext.isEmpty(Ext.getCmp('message_messages').getValue()))
                        return Ext.Msg.alert('请输入留言内容');
                    if (Ext.isEmpty(Ext.getCmp('message_cuid').getValue()))
                        return Ext.Msg.alert('请输入用户ID');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getMessageadd().submit({
                        url: GLOBAL.api_url + 'message_add',
                        scope: this,
                        method: 'POST',
                        success: this.exesucess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * edit message
             */
            message_edit: {
                tap: function () {
                    if (Ext.isEmpty(Ext.getCmp('messageedit_cmid').getValue()))
                        return Ext.Msg.alert('请输入cmid');
                    if (Ext.isEmpty(Ext.getCmp('messageedit_remessage').getValue()))
                        return Ext.Msg.alert('请输入回复');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getMessageedit().submit({
                        url: GLOBAL.api_url + 'message_replay_update',
                        method: 'POST',
                        params: {
                            replytype: '文字'
                        },
                        scope: this,
                        success: this.exesucess
                    });
                    Ext.Viewport.unmask();
                }
            }
        }
    },
    exesucess: function (from, result) {
        if (result.Variables.Result.code == 0) {2
            Ext.getStore('MessageDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
            this.getMessage().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
})
;
